import hashlib

def hash_file(filename):
   """该函数返回传入文件的SHA-1哈希值"""
   # 创建一个哈希对象
   # h = hashlib.sha1()  #20Bytes
   h = hashlib.md5()   #16Bytes
   # 以二进制读取模式打开一个文件
   with open(filename,'rb') as file:
       # 循环直到文件结束
       chunk = 0
       while chunk != b'':
           # read only 1024 bytes at a time
           chunk = file.read(1024)
           h.update(chunk)
   # 返回摘要的十六进制表示
   return h.hexdigest()

# message = hash_file("1.gif")
# print(message)

# #将file2文件写入改动了一个位数的数据
# txt1 = '你好么?我可以用下面这段代码验证一下：'
# txt2 = '你好么?我可以用下面这段代码验证一下：1'
# with open('1.txt', 'w', encoding='utf-8') as f1, open('2.txt', 'w', encoding='utf-8') as f2:
#     f1.write(txt1)
#     f2.write(txt2)


file1_md5 = hash_file('1.txt')
file2_md5 = hash_file('2.txt')
print('file1_md5:',file1_md5)
print('file2_md5:',file2_md5)
if file1_md5 != file2_md5:
    print('file has changed')
    print(hash("file has changed"))
else:
    print("file hasn't changed")

    print(type(hash("d")))
    print(hash("  "))  #hash 值占用8Bytes，其中值为4Bytes,
    print(hash("filechanged"))